package cn.lg.soar.core.util;

import cn.lg.soar.common.data.ZSet;
import cn.lg.soar.common.util.IpUtil;
import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.common.util.hardware.JvmInfo;
import cn.lg.soar.common.util.hardware.SystemUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.ServerProperties;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;

/**
 * @author luguoxiang
 */
@Slf4j(topic = "\u0073\u006f\u0061\u0072")
public class RunnerShowInfo {

    private IpUtil ipUtil;
    private DataUtil dataUtil;
    private JvmInfo jvmInfo;
    private SystemUtils systemUtils;
    private ServerProperties serverProperties;
    private Set set;

    private static ZSet<String> URLS = new ZSet<>();

    /**
     * 自定义显示内容
     * @param text
     */
    public static void showText(String text, int order) {
        if (URLS == null) {
            return;
        }
        URLS.add(text, order);
    }

    public static void showText(String text) {
        showText(text, 0);
    }

    /**
     * 拼接系统路径（title + : http://ip:contextPath + url）
     * @param title 标题
     * @param url 路径
     */
    public static void showUrl(String title, String url) {
        showText(title + ": %s" + url);
    }

    static {
        SoarSubscriber.subscribeRunner((applicationContext) -> {
            List<String> list = new ArrayList<>();
            list.add("lg-soar 启动成功: https://gitee.com/lgx1992/lg-soar");
            // jvm
            try {
                JvmInfo jvmInfo = SystemUtils.getJvmInfo();
                list.add("jvm：" + DataUtil.fileSizeFormat(jvmInfo.getMax()));
                list.add("eden：" + DataUtil.fileSizeFormat(jvmInfo.getEden().getInit()));
                list.add("survivor：" + DataUtil.fileSizeFormat(jvmInfo.getSurvivor().getInit()));
                list.add("old：" + DataUtil.fileSizeFormat(jvmInfo.getOld().getMax()));
            } catch (RuntimeException e) {
                log.error("jvm读取异常", e);
            }
            // ip
            try {
                Set<String> set = URLS;URLS = null;
                ServerProperties serverProperties = applicationContext.getBean(ServerProperties.class);
                String contextPath = serverProperties.getServlet().getContextPath();
                contextPath = contextPath == null ? "" : contextPath;
                String local = String.format("http://localhost:%s%s", serverProperties.getPort(), contextPath);
                list.add("服务地址: " + local);
                set.forEach(x -> list.add(String.format(x, local)));
                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
                while (networkInterfaces.hasMoreElements()) {
                    NetworkInterface networkInterface = networkInterfaces.nextElement();
                    // 可选：跳过回环接口（如 localhost）
                    if (networkInterface.isLoopback() || !networkInterface.isUp()) {
                        continue;
                    }
                    Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                    while (inetAddresses.hasMoreElements()) {
                        InetAddress inetAddress = inetAddresses.nextElement();
                        if (inetAddress instanceof Inet4Address) {
                            String url = String.format("http://%s:%s%s", inetAddress.getHostAddress(), serverProperties.getPort(), contextPath);
                            list.add("服务地址: " + url);
                            set.forEach(x -> list.add(String.format(x, url)));
                        }
                    }
                }
                set.clear();
            } catch (SocketException e) {
                log.error("ip输出异常", e);
            }
            for (String s : list) {
                log.info(s);
            }
        });
    }

}
